package com.example.elm.Account

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AppCompatActivity
import com.example.elm.Public.AccountInfo
import com.example.elm.Public.MyApplication
import com.example.elm.databinding.PasswordChangeBinding
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.net.URLEncoder
import kotlin.concurrent.thread

class PasswordActivity : ComponentActivity() {
    private lateinit var binding: PasswordChangeBinding
    private val server_ip = MyApplication.serverIp
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = PasswordChangeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val AccountInformation = CommonUtil.getSerializable(this, "info", AccountInfo::class.java)
        val old = AccountInformation
        val minLength = 9
        binding.back.setOnClickListener {
            val intent = Intent()
            intent.putExtra("info", old)
            setResult(RESULT_OK, intent)
            finish()
        }

        binding.changeButton.setOnClickListener {
            val new_password = binding.password.text.toString()
            val new_password_confirm = binding.password2.text.toString()

            if (AccountInformation.account == ""){
                Toast.makeText(this, "未登录", Toast.LENGTH_SHORT).show()
                binding.password.text.clear()
                binding.password2.text.clear()
            }
            else if(new_password.length < minLength){
                Toast.makeText(this, "密码过短", Toast.LENGTH_SHORT).show()
                binding.password.text.clear()
                binding.password2.text.clear()
            }
            else if (new_password != new_password_confirm){
                Toast.makeText(this, "两次密码不同", Toast.LENGTH_SHORT).show()
                binding.password2.text.clear()
            }
            else{
                // 更新结果返回到调用 Activity
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show()
                val new = AccountInfo(AccountInformation.account, new_password, AccountInformation.name,
                    AccountInformation.people_name, AccountInformation.head_image, AccountInformation.introduction)
                update_password(new)
                val intent = Intent()
                intent.putExtra("info", new)
                setResult(RESULT_OK, intent)
                finish()
            }
        }
    }
    //连接服务器，更新简介
    fun update_password(info: AccountInfo){
        val jsp = "http://$server_ip:8080/Account/change_Password.jsp"
        thread {
            try{
                val client = OkHttpClient()
                val requestBody = FormBody.Builder()
                    .add("phone", URLEncoder.encode(info.account, "UTF-8"))
                    .add("password", URLEncoder.encode(info.password, "UTF-8"))
                    .build()
                val request: Request = Request.Builder()
                    .url(jsp)
                    .post(requestBody)
                    .build()
            }
            catch (e: Exception){
                e.printStackTrace()
            }
        }
    }
}